// pages/basicInfo/basicInfo.js
const getdata = require("../../tool/GetDate.js")
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: "",
    number: "",
    name: "",
    nickName: "",
    disabled: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      mobile: app.globalData.myUserData.mobile,
      number: app.globalData.myUserData.number,
      name: app.globalData.myUserData.name,
      nickName: app.globalData.userInfo.nickName,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  saveUserInfo: function (jParams) {
    let that = this;
    wx.login({
      success(res) {
        if (res.code) {
          //发起网络请求
          jParams.jscode = res.code;
          console.log(jParams);
          getdata.postdata({
            url: "gbt/UserVerify/SetUser",
            data: JSON.stringify(jParams),
            success: function (res) {
              console.log(res.root);
              if (res.root) {
                wx.setStorage({
                  key: 'myUserData',
                  data: res.root.userInfo.data[0],
                })
                app.globalData.myUserData = res.root.userInfo.data[0];
                wx.showToast({
                  title:'保存成功',
                  icon:'success',
                  duration:2000
                })
              }
            }
          });
        } else {
          wx.hideLoading();
          console.log('保存失败！' + res.errMsg)
        }
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function() {

  // },

  onNumberInput: function (e) {
    console.log(e);
    this.setData({
      number: e.detail.value,
    })
  },

  onNameInput: function (e) {
    console.log(e);
    this.setData({
      name: e.detail.value,
    })
  },

  onConfirm: function () {
    console.log(app.globalData.userInfo);
    let jParams = {};
    jParams.mobile = app.globalData.myUserData.mobile;
    jParams.number = this.data.number;
    jParams.name = this.data.name;
    jParams.nickName = app.globalData.userInfo.nickName;
    jParams.country = app.globalData.userInfo.country;
    jParams.province = app.globalData.userInfo.province;
    jParams.city = app.globalData.userInfo.city;
    jParams.language = app.globalData.userInfo.language;
    jParams.districtID = app.globalData.myUserData.districtID;

    this.saveUserInfo(jParams);
  },
})